package cn.wolfcode.crm.service.imp;

import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.mapper.DepartmentMapper;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.query.QueryObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class DepartmentServiceImpl implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;


    public Department selectByPrimaryKey(Long id) {
        return departmentMapper.selectByPrimaryKey(id);
    }

    public List<Department> selectAll() {
        return departmentMapper.selectAll();
    }


    public PageResult query(QueryObject qo) {
        int total = departmentMapper.selectCount(qo);
        if(total==0){
            return new PageResult(total, Collections.EMPTY_LIST);
        }
        List<Department> rows = departmentMapper.queryForList(qo);
        return new PageResult(total,rows);
    }

    public Boolean selectByName(Long id, String name) {
        Department dept = departmentMapper.selectByName(name);
        return dept==null||dept.getId()==id;
    }

    public void changeState(Long id) {
        departmentMapper.updateStateById(id);
    }

    public void saveOrUpdate(Department dept) {

        if (dept.getId() == null) {
            departmentMapper.insert(dept);
        }else{
            departmentMapper.updateByPrimaryKey(dept);
        }
    }
}
